package kr.hkit.androidcomplete03.ETC;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridPosterTest extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main1);

		final GridView gv = (GridView) findViewById(R.id.postergrid);
		MyGridAdapter gAdapter = new MyGridAdapter(this);
		gv.setAdapter(gAdapter);
	}

	public class MyGridAdapter extends BaseAdapter {
		Context context;

		Integer[] posterID = { R.drawable.mov01, R.drawable.mov02,
				R.drawable.mov03, R.drawable.mov04, R.drawable.mov05,
				R.drawable.mov06, R.drawable.mov07, R.drawable.mov08,
				R.drawable.mov09, R.drawable.mov10, R.drawable.mov11,
				R.drawable.mov12, R.drawable.mov13, R.drawable.mov14,
				R.drawable.mov15, R.drawable.mov16, R.drawable.mov17 };

		String[] posterNameID = { "써니", "완득이", "괴물", "라디오스타", "비열한 거리",
				"왕의 남자", "아일랜드", "웰컴투동막골", "헬보이", "백투더퓨쳐", "여인의 향기", "쥬라기공원",
				"포레스트검프", "제목몰라", "혹성탈출", "아름다운비행", "내이름은칸", };

		public MyGridAdapter(Context context) {
			this.context = context;
		}

		@Override
		public int getCount() {
			return posterID.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageview = new ImageView(context);
			imageview.setLayoutParams(new GridView.LayoutParams(100, 150));
			imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
			imageview.setPadding(5, 5, 5, 5);

			final int pos = position;
			imageview.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					View dialogView = View.inflate(GridPosterTest.this,
							R.layout.dialog1, null);

					AlertDialog.Builder dlg = new AlertDialog.Builder(
							GridPosterTest.this);

					ImageView ivPoster = (ImageView) dialogView
							.findViewById(R.id.ivposter);
					ivPoster.setImageResource(posterID[pos]);
					dlg.setTitle(posterNameID[pos])
							.setIcon(R.drawable.movie_icon).setView(dialogView)
							.setNegativeButton("닫기", null).show();
				}

			});

			imageview.setImageResource(posterID[position]);
			return imageview;
		}

	}
}
